/*
This Procedure Changes the Purchase Order line item Status based on the Status Received Quantity.
Author - Ravikanth
Date   - 21st Feb 05
Usage exec sp_change_purchase_order_status 51
*/

ALTER  PROC sp_change_purchase_order_lineItem_status @POLineItem_ID varchar(17)
AS
BEGIN
	DECLARE @requetedQty INT
	DECLARE @receivedQty INT

	SELECT @requetedQty = Requested_Qty, @receivedQty = Received_Qty FROM PurchaseOrder_Line_Item_Table WHERE POLineItem_Id = @POLineItem_ID
	IF @requetedQty = @receivedQty
	BEGIN
		UPDATE PurchaseOrder_Line_Item_Table SET Status = 'Closed' WHERE POLineItem_Id = @POLineItem_ID
	END
	ELSE IF @receivedQty > 0
	BEGIN
		UPDATE PurchaseOrder_Line_Item_Table SET Status = 'Received' WHERE POLineItem_Id = @POLineItem_ID
	END
	ELSE
	BEGIN
		UPDATE PurchaseOrder_Line_Item_Table SET Status = 'Received' WHERE POLineItem_Id = -1
	END
END